﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MoveToLocation.CCApMoveToLocationApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.CycleTimeReport;
using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.MoveToLocation;

public class CCApMoveToLocationApp : CCApLMLocationsApp
{
  private ITxRoboticLocationOperation m_roboticLocOp;
  private TxSimulationPlayer m_player;

  public CCApMoveToLocationApp() => this.InitLocation();

  public void SelectRoboticLocationOperation(ITxRoboticLocationOperation locOp)
  {
    TxApplication.ActiveSelection.Clear();
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) locOp);
    TxApplication.ActiveSelection.SetItems(txObjectList);
  }

  private void InitLocation()
  {
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (ITxRoboticLocationOperation));
    txTypeFilter.AddIncludedType(typeof (ITxRoboticOrderedCompoundOperation));
    txTypeFilter.AddIncludedType(typeof (TxRoboticSeamOperation));
    TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) txTypeFilter);
    if (((Collection<ITxObject>) filteredItems).Count != 1)
      return;
    ITxObject itxObject = ((Collection<ITxObject>) filteredItems)[0];
    switch (itxObject)
    {
      case ITxRoboticLocationOperation _:
        this.m_roboticLocOp = itxObject as ITxRoboticLocationOperation;
        break;
      case TxRoboticSeamOperation _:
        TxRoboticSeamOperation roboticSeamOperation1 = itxObject as TxRoboticSeamOperation;
        if (roboticSeamOperation1.Count <= 0)
          break;
        this.m_roboticLocOp = roboticSeamOperation1.GetChildAt(0) as ITxRoboticLocationOperation;
        if (this.m_roboticLocOp == null)
          break;
        this.SelectRoboticLocationOperation(this.m_roboticLocOp);
        break;
      case ITxRoboticOrderedCompoundOperation _:
        if (itxObject is ITxContinuousOperation)
        {
          ITxObject childAt = ((ITxOrderedObjectCollection) (itxObject as ITxContinuousOperation)).GetChildAt(0);
          if (childAt is TxRoboticSeamOperation)
          {
            TxRoboticSeamOperation roboticSeamOperation2 = childAt as TxRoboticSeamOperation;
            if (roboticSeamOperation2.Count > 0)
              this.m_roboticLocOp = roboticSeamOperation2.GetChildAt(0) as ITxRoboticLocationOperation;
          }
          else
            this.m_roboticLocOp = childAt as ITxRoboticLocationOperation;
        }
        else
          this.m_roboticLocOp = ((ITxOrderedObjectCollection) (itxObject as ITxRoboticOrderedCompoundOperation)).GetChildAt(0) as ITxRoboticLocationOperation;
        if (this.m_roboticLocOp == null)
          break;
        this.SelectRoboticLocationOperation(this.m_roboticLocOp);
        break;
    }
  }

  public void Play()
  {
    this.InitLocation();
    bool ignoreSignals = TxApplication.Options.Simulation.IgnoreSignals;
    TxSimulationPlayer.IgnoreSignals = true;
    CApCTRToggleCmd capCtrToggleCmd = new CApCTRToggleCmd();
    bool activate = capCtrToggleCmd.IsCycleTimeReportActivated();
    capCtrToggleCmd.ActivateCycleTimeReport(false);
    if (this.m_roboticLocOp != null)
    {
      if (this.m_player == null)
        new TxSimulationPlayer().SetOperation((ITxOperation) this.m_roboticLocOp);
      this.m_player = new TxSimulationPlayer(true, true);
      TxSimulationPlayerEx.DisableReset(this.m_player);
      this.m_player.SetOperation((ITxOperation) this.m_roboticLocOp);
      bool flag = false;
      if (!TxFeatureManager.CheckFeature("DISABLE_RRS_MULTITHREAD_BEHAVIOUR"))
      {
        flag = true;
        TxFeatureManager.UpdateFeature("DISABLE_RRS_MULTITHREAD_BEHAVIOUR", false);
      }
      this.m_player.Play();
      this.m_player.Stop();
      this.m_player.AskUserForReset(true);
      if (flag)
        TxFeatureManager.UpdateFeature("DISABLE_RRS_MULTITHREAD_BEHAVIOUR", true);
    }
    else
    {
      int num = (int) TxMessageBox.Show(new ResourceManager("DnProcessSimulateCommands.MoveToLocation.StringTable", this.GetType().Assembly).GetString("ERROR_NO_LOCATION"), "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    TxSimulationPlayer.IgnoreSignals = ignoreSignals;
    capCtrToggleCmd.ActivateCycleTimeReport(activate);
    if (this.m_player == null)
      return;
    this.m_player.ResetToDefaultSetting();
  }

  public void Stop()
  {
    if (this.m_player == null)
      return;
    this.m_player.Stop();
  }
}
